28. Tips, Tricks, and Other Notes
Watch those shapes
In general, you'll want to check that the tensors going through your model and other code are the correct shapes. Make use of the .shape
method during debugging and development.
A few things to check if your network isn't training appropriately
Make sure you're clearing the gradients in the training loop with optimizer.zero_grad()
. If you're doing a validation loop, be sure to set the network to evaluation mode with model.eval()
, then back to training mode with model.train()
.
CUDA errors
Sometimes you'll see this error:
RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #1 ‘mat1’
You'll notice the second type is torch.cuda.FloatTensor
, this means it's a tensor that has been moved to the GPU. It's expecting a tensor with type torch.FloatTensor
, no .cuda
there, which means the tensor should be on the CPU. PyTorch can only perform operations on tensors that are on the same device, so either both CPU or both GPU. If you're trying to run your network on the GPU, check to make sure you've moved the model and all necessary tensors to the GPU with .to(device)
where device
is either "cuda"
or "cpu"
.